package com.zwh.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 类
 *
 * @author jiangxianliang
 * @since 2021/4/27
 */
@Component
public class MyBatisPlusHandler implements MetaObjectHandler {


	/**
	 * 配置新增自动生成字段
	 * @param metaObject
	 */
	@Override
	public void insertFill(MetaObject metaObject) {
		this.strictInsertFill(metaObject, "creatorId", Long.class, IdWorker.getId());
		this.strictInsertFill(metaObject, "creatorName", String.class, "userName");
		this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
		this.strictInsertFill(metaObject, "tenantOid", Long.class, 000000L);

	}

	/**
	 * 配置更新自动生成字段
	 * @param metaObject
	 */
	@Override
	public void updateFill(MetaObject metaObject) {
		this.strictUpdateFill(metaObject, "updatorId", Long.class,IdWorker.getId());
		this.strictUpdateFill(metaObject, "updatorName", String.class,"userName");
		this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
	}

	/***
	 * 初始化mybatisPlus插件
	 * @return
	 */
	public MybatisPlusInterceptor initMybatisPlusInterceptor(){
		MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
		mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
		return mybatisPlusInterceptor;
	}

}
